import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/WorkbenchPage.dart';
import 'package:im_app/im/page/bloc/SessionListCubit.dart';
import 'package:im_app/im/page/view/WebRtcFloatingView.dart';
import 'package:im_app/im/util/ImSingleton.dart';
import '../im/page/DepartmentListPage.dart';
import '../im/page/SessionListPage.dart';
import '../im/page/UserAndGroupPage.dart';
import '../im/page/bloc/WebRtcCubit.dart';
import '../im/page/view/BadgeView.dart';
import '../im/page/view/DraggableFloatingView.dart';
import '../im/page/view/component/Common.dart';
import '../im/pojo/ImPojo.dart';
import '../im/util/ImConstant.dart';
import '../im/page/MePage.dart';

class MainPage extends StatefulWidget {
	final String title;

	const MainPage({super.key, required this.title});

	@override
	State<StatefulWidget> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
	int _selectedIndex = 0;
	PageController? _pageController;
	late ImUser _loginUser;

	void _onItemTapped(int index) {
		setState(() {
		  _selectedIndex = index;
		  _pageController?.jumpToPage(index);
		});
		if (index != 0) {
			ImSingleton.getInstance().cleanUiModel();
		}
	}

	@override
	void initState() {
		ImSingleton.getInstance().mainContext = context;
		super.initState();
		_pageController = PageController();
		_loginUser = ImSingleton.getInstance().session.getData("LoginUser") as ImUser;
	}

	@override
	void dispose() async {
		_pageController?.dispose();
		super.dispose();
	}

	@override
	Widget build(BuildContext context) {
		var mainAppBars = <AppBar>[createSessionAppBar(context, "宇连通"), createSessionAppBar(context, "通讯录"),
			createSessionAppBar(context, "部门机构"), createSessionAppBar(context, "工作台"), MeAppBar];
		return  Scaffold(
				appBar: mainAppBars.elementAt(_selectedIndex),
				backgroundColor: ImConstant.IM_RESERVE_BACKGROUND_COLOR,
				body: Stack(
					children: [
						PopScope(
							canPop: false,
							child: PageView(
								onPageChanged: _onItemTapped,
								controller: _pageController,
								children: <Widget>[
									const SessionListPage(),
									const UserAndGroupPage(),
									DepartmentListPageCubit(),
									const WorkbenchPage(),
									MePage(loginUser: _loginUser)
								],
							),
						),
					],
				),
				bottomNavigationBar: BottomNavigationBar(
					backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
					items: [
						BottomNavigationBarItem(icon: _buildImFirstWidget(context), label: 'IM'),
						BottomNavigationBarItem(icon:
							Icon(Icons.person_outline_sharp, color: _selectedIndex == 1 ? Colors.green : Colors.black),
								label: '通信录'
						),
						BottomNavigationBarItem(icon: _selectedIndex == 2 ?
							Image.asset("assets/img/mechanism1.png", width: 30, height: 19) :
						  Image.asset("assets/img/mechanism.png", width: 30, height: 19),
								label: '部门机构'
						),
						BottomNavigationBarItem(icon:
							Icon(Icons.account_balance,
								color: _selectedIndex == 3 ? Colors.green : Colors.black),
								label: '工作台'
						),
						BottomNavigationBarItem(icon:
							Icon(Icons.person_outline,
								color: _selectedIndex == 4 ? Colors.green : Colors.black),
								label: '我'
						),
					],
					iconSize: 20,
					type: BottomNavigationBarType.fixed,
					currentIndex: _selectedIndex,
					selectedItemColor: Colors.green,
					onTap: _onItemTapped,
				),
		);
	}

	Widget _buildImFirstWidget(BuildContext context) {
		// SessionListCubit sessionListCubit = context.watch<SessionListCubit>();
		// ImSingleton.getInstance().hotMessageTotalCubit = sessionListCubit.state.hotMessageTotal;
		return BlocSelector<SessionListCubit, SessionListState, int>(selector: (state)=>state.hotMessageTotal
			, builder: (BuildContext context, hotMessageTotal) {
			// var total = hotMessageTotal >= 99 ? 99 : hotMessageTotal;
			var total = hotMessageTotal;
			return BadgetView(label: "$total",
				showBadge: total>0,
				child: Padding(padding: const EdgeInsets.only(top: 5, right: 5),
					child: Icon(Icons.message_outlined, color: _selectedIndex == 0 ? Colors.green : Colors.black),),
				);
		});
	}
}
